// plugin/pages/home/home.js
import { emitCb, getOption } from "..";
import { loginRequest } from "../utils/request";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    args: {
      withCredentials: true,
      lang: "zh_CN",
    },
    option: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const option = getOption();
    this.setData({ option });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },

  async getPhoneNumber(e) {
    // open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"
    const loginRes = await wx.login();
    const code = loginRes.code;
    console.log("loginRes===", loginRes);
    console.log("getPhoneNumber", e);
    const { encryptedData, errMsg, iv } = e.detail;
    if (errMsg.includes('getPhoneNumber:ok')) {

      this.requestGizLogin({ code, encryptedData, iv });
    }
  },

  async getUserProfile() {
    console.log("onClickWxLogin");
    let nickName, avatarUrl;
    try {
      const infoRes = await wx.getUserProfile({ ...this.data.args, desc: '登录账号' });
      console.log("infoRes===", infoRes);
      if (infoRes.errMsg == 'getUserProfile:ok') {
        const { userInfo } = infoRes;
        nickName = userInfo.nickName;
        avatarUrl = userInfo.avatarUrl;
        // wx.showToast({
        //   title: '授权失败',
        //   icon: "error",
        // });
        // return;
      }

    } catch (e) {

    }

    const loginRes = await wx.login();
    const code = loginRes.code;
    console.log("loginRes===", loginRes);
    // wx.login({
    // complete: (result) => {
    // console.log("result:", result);
    // wx.getUserProfile({
    //   // ...this.data.args,
    //   desc: '需要用到头像',
    //   complete: (res) => {
    //     console.log("res", res);
    //   }
    // });
  // }
    // })
    // const loginRes = await wx.login();
    // console.log("loginRes===", loginRes);
    // const code = loginRes.code;
    // console.log("this.data.args===", this.data.args);

    // const userInfo = await wx.getUserProfile({ ...this.data.args, desc: '需要用到头像' });
    // console.log("userInfo===", userInfo);
    // const { encryptedData, iv } = userInfo;

    this.requestGizLogin({ code, nickName, avatarUrl });
  },
  requestGizLogin: async function ({ code, encryptedData, iv, nickName, avatarUrl }) {
    // 新：/app/applets/login
    // 旧： app/applets/thirdLogin
    const loginRes = await loginRequest(`app/applets/thirdLoginUser?code=${code}`, {
      encryptedData,
      iv,
      // nickname: nickName,
      // avatar: avatarUrl,
    });

    const { code: status, message, data } = loginRes.data;
    if (status != 200) {
      wx.showToast({
        title: message,
        icon: "error",
      });
    } else {
      wx.showToast({
        title: "登录成功",
        icon: "success",
      });
      emitCb(data);
      setTimeout(() => {
        wx.navigateBack({ delta: 1 });
      }, 1000);
    }
  },
});
